package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.CNews;
import com.ruoyi.system.service.ICNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CCategoryMapper;
import com.ruoyi.system.domain.CCategory;
import com.ruoyi.system.service.ICCategoryService;
import com.ruoyi.common.core.text.Convert;

/**
 * 资讯类别Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-05-22
 */
@Service
public class CCategoryServiceImpl extends ServiceImpl<CCategoryMapper, CCategory> implements ICCategoryService
{
    @Autowired
    private CCategoryMapper cCategoryMapper;
    @Autowired
    private ICNewsService icNewsService;

    /**
     * 查询资讯类别
     * 
     * @param id 资讯类别ID
     * @return 资讯类别
     */
    @Override
    public CCategory selectCCategoryById(Long id)
    {
        return cCategoryMapper.selectCCategoryById(id);
    }

    /**
     * 查询资讯类别列表
     * 
     * @param cCategory 资讯类别
     * @return 资讯类别
     */
    @Override
    public List<CCategory> selectCCategoryList(CCategory cCategory)
    {
        return cCategoryMapper.selectCCategoryList(cCategory);
    }

    /**
     * 新增资讯类别
     * 
     * @param cCategory 资讯类别
     * @return 结果
     */
    @Override
    public int insertCCategory(CCategory cCategory)
    {
        cCategory.setCreateTime(DateUtils.getNowDate());
        return cCategoryMapper.insertCCategory(cCategory);
    }

    /**
     * 修改资讯类别
     * 
     * @param cCategory 资讯类别
     * @return 结果
     */
    @Override
    public int updateCCategory(CCategory cCategory)
    {
        cCategory.setUpdateTime(DateUtils.getNowDate());
        return cCategoryMapper.updateCCategory(cCategory);
        //return this.saveOrUpdate(cCategory);
    }

    /**
     * 删除资讯类别对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCCategoryByIds(String ids)
    {
        return cCategoryMapper.deleteCCategoryByIds(Convert.toStrArray(ids));
//        return this.removeByIds(Arrays.asList(Convert.toStrArray(ids)));
    }

    /**
     * 删除资讯类别信息
     * 
     * @param id 资讯类别ID
     * @return 结果
     */
    @Override
    public int deleteCCategoryById(Long id)
    {
        int count = this.count(new QueryWrapper<CCategory>().eq("parent_id",id));
        if(count > 0){
            throw new BusinessException("当前分类有子分类，不允许删除");
        }
        count = icNewsService.count(new QueryWrapper<CNews>().eq("cate_id",id));
        if(count > 0){
            throw new BusinessException("当前分类有子内容，不允许删除");
        }
        return cCategoryMapper.deleteCCategoryById(id);
        //return this.removeById(id);
    }

    /**
     * 查询资讯类别树列表
     * 
     * @return 所有资讯类别信息
     */
    @Override
    public List<Ztree> selectCCategoryTree()
    {
        List<CCategory> cCategoryList = cCategoryMapper.selectCCategoryList(new CCategory());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (CCategory cCategory : cCategoryList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(cCategory.getId());
            ztree.setpId(cCategory.getParentId());
            ztree.setName(cCategory.getCateName());
            ztree.setTitle(cCategory.getCateName());
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
